#!/usr/bin/perl 
use strict;

use DBI;
use CGI;
use TTraker;

my $dbh;

initialize_dbi();
print_header();

my $form_ref = &parse_input;

my %FORM_DATA = %$form_ref; 

my $num = $FORM_DATA{'empNum'};
my $name = $FORM_DATA{'empName'};
my $job_id = $FORM_DATA{'empJob'};
my $loc = $FORM_DATA{'empLoc'};
my $activity = $FORM_DATA{'activity'};
my $sent_from = $ENV{'REMOTE_ADDR'};

my $now = get_local_time($loc);

my @active = qw (blank start finish log continue lunch break leave);
print_log ("$activity,$active[$activity],trak_02_activiyty.pl,$num,$name");

if ($activity == 1 ) {
	my $session = get_new_count ('SESSION');
	start_job_screen_from_pm  ($num, $name, $job_id, $loc, $activity, $session);	
} elsif ($activity == 2 ) { 
   close_job ($num, $name, $job_id, $loc, $activity, $now);
} elsif ($activity == 3 ) {
   log_tasks ($num, $name, $job_id, $loc, $activity, $now);
} elsif ($activity == 4 ) {
   continue_job ($num, $name, $job_id, $loc, $activity, $now);
} elsif ($activity == 5 ) {
   lunch_time ($num, $name, $job_id, $loc, $activity, $now);
} elsif ($activity == 6 ) {
   break_time ($num, $name, $job_id, $loc, $activity, $now);
} elsif ($activity == 7 ) {
   leave_work ($num, $name, $job_id, $loc, $activity,$now);
} elsif ($activity == 8 ) {
   select_batch_jobs ($num, $name, $job_id, $loc, $activity,$now);
} else {
   print "ACTIVITY ERROR!<br/>";
}

db_disconnect();

############### SUBROUTINES FOLLOW ###############

sub select_batch_jobs
{
	my $num = $_[0];
	my $name = $_[1];
	my $job_id = $_[2];
	my $loc = $_[3];
	my $activity = $_[4];
	my $now = $_[5];
	my $batchNum = get_new_count ('BATCH');
	my $open_cli = get_open_job_client ($num);
#	print "THE OPEN CLIENT IS $open_cli and the group num is $groupNum<br/>";
	my $hold_count = get_hold_client_count ($num, $open_cli);
#	print "OPEN CLIENT $open_cli Client PLUS HOLDS! $hold_count<br/>";
	if ( $hold_count == 0 ) {
				my $warning = "NO JOBS ON HOLD FOR $open_cli!";
				activity_screen_from_pm ($num, $name, $job_id, $loc, $warning);
	} else {
		my $holdList_ref = get_hold_job_list ($num, $open_cli);
		my @holdList = @$holdList_ref;
		batch_job_select_from_pm ($num, $name, $job_id, $loc, $batchNum, $holdList_ref, $activity);
	}	
}



sub close_job
{
	my $num = $_[0];
	my $name = $_[1];
	my $job_id = $_[2];
	my $loc = $_[3];
	my $activity = $_[4];
	my $now = $_[5];
	my $warning = "";
	my $job_count = get_open_job_count ($num);
	if ( $job_count == 0 ) {
		$warning = "NO OPEN JOB FOR $name! Nothing to close!";
		activity_screen_from_pm ($num, $name, $job_id, $loc, $warning);
#		trak_02_activity_selection_message ($num, $name, $job_id, $loc, $activity, $warning);
	} elsif ( $job_count == 1 ) {
		my $job_ref = get_open_job ($num);
		my ($doc,$job,$cli,$stmp) = @$job_ref;
		if ($cli eq "LUNCH" || $cli eq "BREAK" || $cli eq "IDLE") {
      	$warning = "CURRENT ACTIVITY: $cli! No job to close!";
			activity_screen_from_pm ($num, $name, $job_id, $loc, $warning);
	   } else {
   		update_task_std_time ($job);
			log_job_event ($job, $now);
	   	close_current_job ($job);
   		create_idle_event ($num, $now);
   		update_job_headers_with_time ($job);
     	}
	} else {
		# DO NADDA
	}
	activity_screen_from_pm ($num, $name, $job_id, $loc, $warning);
}

sub start_job
{
my $num = $_[0];
my $name = $_[1];
my $job_id = $_[2];
my $loc = $_[3];
my $activity = $_[4];
my $session = $_[5];

my $now = get_local_time($loc);

my $client_ref = get_clients(); 
my @clients = @$client_ref;
print <<ENDHEADER;
<html>
<head>
<link rel='stylesheet' type='text/css' href='${css_ref}trak.css' />
<script type='text/javascript' src='${js_ref}trak.js'></script>
<title>trak_02_doc_num.html</title>
</head>
<body onload='startUp1()'>
<h2><center>${banner}</center></h2>
<hr/>
<div id='clock'></div>
<div id='box1'>

Employee Number:<br/><br/>
Employee Name:<br/><br/>
Document Number:<br/><br/>
Client ID:<br/><br/>
</div>
<form id='box2' autocomplete='off' name='startJob' action='${cgi_ref}trak_03_initiate_job.pl' method='post'>
<input type="hidden" name=empNum value='$num'/>
<input type="hidden" name=empName value='$name'/>
<input type="hidden" name=empJob value='$job_id'/>
<input type="hidden" name=empLoc value='$loc'/>
<input type="hidden" name=empAct value='$activity'/>
<input type="hidden" name=empSes value='$session'/>
<span>$num</span><br/><br/>
<span>$name</span></b><br/><br/>
<input type="text" name="docNum" size=10 tabindex=1 /><br/><br/>
<select id=clientDrop name="cliId" tabindex=2 >

ENDHEADER

# add a blank element at the start of the list
print "<option value='NUL'></option>";
# add all clients on the list to the drop down
foreach my $cli (@clients) {
   print "<option value=$cli >$cli</option>";
}

print <<ENDHEADER;

</select>
<br/><br/>
<input type='submit' name='submit' value='Start New Job'>
<br/><br/>
<a href="${cgi_ref}trak_00_start_up.pl?num=$num">Return to Log-In</a>
</form>
</body>
</html>

ENDHEADER
}

sub log_tasks
{
	my $num = $_[0];
	my $name = $_[1];
	my $job_id = $_[2];
	my $loc = $_[3];
	my $activity = $_[4];
	my $time = 	 $_[5];
	my $code = "TASKS";
	start_system_time ($num, $code, $now);

	my ($warning, $task_count, $task_ref, $doc, $job, $task, $emp, $cli, $stmp);

	my $job_count = get_open_job_count ($num);

	if ( $job_count == 0 ) {
		$warning = "NO OPEN JOB FOR $name! Can not log tasks.";
		activity_screen_from_pm ($num, $name, $job_id, $loc, $warning);
	} elsif ( $job_count == 1 ) {
		my $job_ref = get_open_job ($num);
		($doc,$job,$emp,$cli,$stmp) = @$job_ref;
		if ($cli eq "LUNCH" || $cli eq "BREAK" || $cli eq "IDLE") {
      		$warning = "CURRENT ACTIVITY: $cli! Can not log tasks!";
			activity_screen_from_pm ($num, $name, $job_id, $loc, $warning);
      } else {
      		$task_count = get_task_count ($cli, $job_id, "inbound");
      		if ($task_count > 0 ) {
      	  		trak_02_log_tasks_screen ($num, $name, $job_id, $loc, $activity, $cli,$stmp, $doc, $job, $task );
      		} else {
	      		$warning = "NO TASKS FOUND FOR $cli! See Administrator!";
					activity_screen_from_pm ($num, $name, $job_id, $loc, $warning);
      			# do nadda
      		}
      }
	} else {
		# DO NADDA
	}	
# print "WE ARE DONE!<br/>";
}

sub trak_02_log_tasks_screen
{
#	print "OLA!<br/>";
	my $num = $_[0];
	my $name = $_[1];
	my $job_id = $_[2];
	my $loc = $_[3];
	my $activity = $_[4];
	my $cli = $_[5];
	my $stmp = $_[6];
	my $doc = $_[7];
	my $job = $_[8];
	my $task = $_[9];

	my $button_text;
	if ( $activity == 2 ) {
		$button_text = "Log Tasks - close job";
	} elsif ($activity == 3 ) {
		$button_text = "Log Tasks - keep job open";
	}	else {
		$button_text = "Log Tasks - ????";
	} 
	my $task_ref = get_task_list ($cli, $job_id, "outbound");
 	my @tasks = @$task_ref;
	print <<ENDHEADER;
	<html>
	<head>
	<link rel='stylesheet' type='text/css' href='${css_ref}trak.css' />
	<script type='text/javascript' src='${js_ref}trak.js'></script>
	<title>trak_02_activity_log_tasks.pl</title>
	</head>
	<body onload='startTime()'>
	<h2><center>${banner}</center></h2>
	<hr/>
	<div id='clock'></div>
	<div id='box1'>
	Employee Number:<br/>
	Employee Name:<br/>
	Client:<br/>
	Document ID:<br/>
	</div>
	<div id='box2'>
	<span>$num</span><br/>
	<span>$name</span><br/>
	<span>$cli</span><br/>
	<span>$doc</span><br/><br/>
	</div>
	<form autocomplete='off' name='select_area' action='${cgi_ref}trak_05_log_tasks_requery.pl' method='post'>
	<div id="box1_2">
	<input type="hidden" name="empNum" value='$num'/>
	<input type="hidden" name="empName" value='$name'/>
	<input type="hidden" name="empJob" value='$job_id'/>
	<input type="hidden" name="empLoc" value='$loc'/>
	<input type="hidden" name=docNum value='$doc'/>
	<input type="hidden" name=jobNum value='$job'/>
	<input type="hidden" name=taskNum value='$task'/>
	<input type="hidden" name=cliId value='$cli'/>
	<input type="hidden" name=stamp value='$stmp'/>
	<input type="hidden" name="activity" value='$activity'/>
	<label><input type="radio" name="requery" value="inbound" /> Inbound</label><br/>
	<label><input type="radio" name="requery" value="outbound" checked="checked" /> Outbound </label><br/>
	<label><input type="radio" name="requery" value="include" /> All  &nbsp&nbsp</label><br/>
	<input type='submit' value='Select'>
	<br/>
	</div>        
	</form>
	<div id='box5'>
	<form onkeypress='return event.keyCode != 13;' autocomplete='off'  name='activity' action='${cgi_ref}trak_05_log_tasks.pl' method='post'>
	<input type="hidden" name="empNum" value='$num'/>
	<input type="hidden" name="empName" value='$name'/>
	<input type="hidden" name="empJob" value='$job_id'/>
	<input type="hidden" name="empLoc" value='$loc'/>
	<input type="hidden" name=docNum value='$doc'/>
	<input type="hidden" name=jobNum value='$job'/>
	<input type="hidden" name=taskNum value='$task'/>
	<input type="hidden" name=cliId value='$cli'/>
	<input type="hidden" name=stamp value='$stmp'/>
	<input type="hidden" name="activity" value='$activity'/>
	<table>
	<tr>
		<th id='th_right'>Activity</th>
		<th></th>
		<th>Units</th>
	</tr>
ENDHEADER
	for my $i (0..$#tasks ) {
		my $code=$tasks[$i][0];
		my $desc = $tasks[$i][1];
		$desc =~ tr/A-Z/a-z/;
		my $units = $tasks[$i][2];
		$units =~ tr/A-Z/a-z/;
print <<ENDHEADER;
	<tr>
 	<td id='description1' >${desc} &nbsp&nbsp </td>
		<td><input type='text' size='4' name='task_${code}'/></td>
		<td>&nbsp&nbsp&nbsp $units</td>
		</tr>
ENDHEADER
	}
	print <<ENDHEADER;
	</table>
	<br/><br/>
	<input type='submit' value='$button_text'>
	<br/><br/>
	<a href="${cgi_ref}trak_00_start_up.pl?num=$num">Return to Log-In</a>
	<br/><br/>
   </form>
   </div>
   </body>
   </html>
ENDHEADER
}



sub leave_work 
{
	my $num = $_[0];
	my $name = $_[1];
	my $job_id = $_[2];
	my $loc = $_[3];
	my $activity = $_[4];
	my $now = $_[5];
	# print "$num, $name, $job_id, $loc, $activity <br/>";
	log_out_of_work ($num, $now);
	end_system_time ($num, "ALL", $now);
	update_system_elapsed_time ($num );
	log_in_screen_from_pm ();
	
}

sub lunch_time
{
	my $num = $_[0];
	my $name = $_[1];
	my $job_id = $_[2];
	my $loc = $_[3];
	my $activity = $_[4];
	my $now = $_[5];
	# print "$num, $name, $job_id, $loc, $activity <br/>";
	log_lunch_break ($num, $now); 
	end_system_time ($num, "ALL", $now);
	update_system_elapsed_time ($num );
	log_in_screen_from_pm ();
}

sub break_time
{
	my $num = $_[0];
	my $name = $_[1];
	my $job_id = $_[2];
	my $loc = $_[3];
	my $activity = $_[4];
	my $now = $_[5];
	# print "$num, $name, $job_id, $loc, $activity <br/>";
	log_break_time ($num, $now);
	end_system_time ($num, "ALL", $now);
	update_system_elapsed_time ($num );
	log_in_screen_from_pm ();
}

sub continue_job
{
	my $num = $_[0];
	my $name = $_[1];
	my $job_id = $_[2];
	my $loc = $_[3];
	my $activity = $_[4];
	my $now = $_[5];
	my $jobs_ref;
	my @jobs=();
	my $warning;
	# print "$num, $name, $job_id, $loc, $activity <br/>";
   my $job_count = get_paused_job_count ($num);
   if ( $job_count > 0 ) {
      select_job_continue ($num, $name, $job_id, $loc, $activity);
   } else {
      $warning = "NO JOBS ON HOLD FOR $name!";
      & trak_02_activity_selection_message ($num, $name, $job_id, $loc, $activity);
   }   
}

sub trak_02_emp_log_in
{
	print <<ENDHEADER;
	<html>
	<head>
	<link rel="stylesheet" type="text/css" href="${css_ref}trak.css" />
	<script type="text/javascript" src="${js_ref}trak.js"></script>
	<title>trak_01_emp_num.html NO NUM</title>
	</head>
	<body onload="startTime()">
	<h2><center>${banner}</center></h2>
	<hr/>
	<div id="clock"></div>
	<div id="box1">
   &nbsp&nbsp&nbsp&nbsp&nbsp Employee ID: <br/><br/>
	</div> 
	<form id="box2" autocomplete='off' name="empID" action="${cgi_ref}trak_01_emp_log_in.pl" method="post">
   <input type="text" name=empID />&nbsp&nbsp<br/><br/><br/>
   <input type="submit" value="Log Into system">
	</form>
	</body>
	</html>
ENDHEADER
}

sub select_job_continue 
{
	my $num = $_[0];
	my $name = $_[1];
	my $job_id = $_[2];
	my $loc = $_[3];
	my $activity = $_[4];  
 	my $jobs_ref = get_paused_jobs ($num);
	my @jobs = @$jobs_ref;
	print <<ENDHEADER;
	<html>
	<head>
	<link rel='stylesheet' type='text/css' href='${css_ref}trak.css' />
	<script type='text/javascript' src='${js_ref}trak.js'></script>
	<title>trak_02_doc_num.html</title>
	</head>
	<body onload='startTime()'>
	<h2><center>${banner}</center></h2>
	<hr/>
	<div id='clock'></div>
	<div id='box1'>
	Employee Number:<br/><br/>
	Employee Name:<br/><br/>
	Paused Jobs:<br/><br/>
	</div>
	<form id='box2' autocomplete='off' name='activity' action='${cgi_ref}trak_04_continue_job.pl' method='post'>
	<input type="hidden" name=empNum value='$num'/>
	<input type="hidden" name=empName value='$name'/>
	<input type="hidden" name="empJob" value='$job_id'/>
	<input type="hidden" name="empLoc" value='$loc'/>
	<span>$num</span><br/><br/>
	<span>$name</span><br/><br/>
	<select id=jobDrop name="jobId">
ENDHEADER
	foreach my $job (@jobs) {
   	print "$job";
	}
	print <<ENDHEADER;
	</select>
	<br/><br/>
	<input type='submit' value='Continue Paused Job'>
	<br/><br/>
	<a href="${cgi_ref}trak_00_start_up.pl?num=$num">Return to Log-In</a>
	</form>
	</body>
	</html>
ENDHEADER
}


sub trak_02_activity_selection
{
my $num = $_[0];
my $name = $_[1];
my $job_id = $_[2];
my $loc = $_[3];

# print "trak_02_activity_selection</br>";

my $msg = get_message ($num);

my $dis_ref = get_disabled_list ($num);
my @dis = @$dis_ref;

my $col_ref = get_dimmed_list ($num);
my @col = @$col_ref;

print <<ENDHEADER;
<html>
<head>
<link rel='stylesheet' type='text/css' href='${css_ref}trak.css' />
<script type='text/javascript' src='${js_ref}trak.js'></script>
<title>trak_02_activity_select</title>
</head>
<body onload='startTime()'>
<h2><center>${banner}</center></h2>
<hr/>
<div id='clock'></div>
<div id='box1'>
Employee Number: <br/><br/>
Employee Name: <br/><br/>
Activity:
</div>
<form id='box2' autocomplete='off' name='activity' action='${cgi_ref}trak_02_activity.pl' method='post'>
	<input type="hidden" name="empNum" value='$num'/>
	<input type="hidden" name="empName" value='$name'/>
	<input type="hidden" name="empJob" value='$job_id'/>
	<input type="hidden" name="empLoc" value='$loc'/>
<span>$num</span><br/><br/>
<span>$name</span></b><br/><br/>
<input type="radio" name="activity" value="1" checked="checked"/>&nbsp<$col[1]> Start Job</$col[1]><br/>
<input $dis[2] type="radio" name="activity" value="2"/>&nbsp<$col[2]> Close Job (do not log tasks)</$col[2]><br/>
<input $dis[3] type="radio" name="activity" value="3"/>&nbsp<$col[3]> Log Tasks</$col[3]><br/>
<input $dis[4] type="radio" name="activity" value="4"/>&nbsp<$col[4]> Continue Job</$col[4]><br/>
<input $dis[5] type="radio" name="activity" value="5"/>&nbsp<$col[5]> Lunch</$col[5]><br/>
<input $dis[6] type="radio" name="activity" value="6"/>&nbsp<$col[6]> Break</$col[6]><br/>
<input type="radio" name="activity" value="7"/><$col[7]>&nbsp Leave Work</$col[7]><br/>
<br/>
<input type='submit' value='Log Activity'>
<br/><br/>
<a href="${cgi_ref}trak_00_start_up.pl?num=$num">Return to Log-In</a>
<br/><br/>
<b id="info">$msg</b>
</form>
</body>
</html>

ENDHEADER
}


sub trak_02_activity_selection_message
{
my $num = $_[0];
my $name = $_[1];
my $job_id = $_[2];
my $loc = $_[3];
my $activity = $_[4];
my $warning = $_[5];
my $message = &get_message ($num);
print <<ENDHEADER;
<html>
<head>
<link rel='stylesheet' type='text/css' href='${css_ref}trak.css' />
<script type='text/javascript' src='${js_ref}trak.js'></script>
<title>trak_02_doc_num.html</title>
</head>
<body onload='startTime()'>
<h2><center>${banner}</center></h2>
<hr/>
<div id='clock'></div>
<div id='box1'>
Employee Number: <br/><br/>
Employee Name: <br/><br/>
Activity:
</div>
<form id='box2' autocomplete='off' name='activity' action='${cgi_ref}trak_02_activity.pl' method='post'>
<input type="hidden" name=empNum value='$num'/>
<input type="hidden" name=empName value='$name'/>
<span>$num</span><br/><br/>
<span>$name</span></b><br/><br/>
<input type="radio" name="activity" value="1" checked="checked"/>&nbsp Start Job<br/>
<input type="radio" name="activity" value="2"/>&nbsp Finish Job<br/>
<input type="radio" name="activity" value="3"/>&nbsp Log Tasks<br/>
<input type="radio" name="activity" value="4"/>&nbsp Continue Job<br/>
<input type="radio" name="activity" value="5"/>&nbsp Lunch<br/>
<input type="radio" name="activity" value="6"/>&nbsp Break<br/>
<input type="radio" name="activity" value="7"/>&nbsp Leave Work<br/>
<br/>
<input type='submit' value='Log Activity'>
<br/><br/>
<a href="${cgi_ref}trak_00_start_up.pl?num=$num">Return to Log-In</a>
<br/><br/>
<b id="warn">$warning</b><br/><br/>
<b id="info">$message</b><br/><br/>
</form>
</body>
</html>
ENDHEADER
}




